﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace ModelResourceManager.ResourceModel
{
    public class EntityResourceManager
    {
        public bool Add(Resource resource)
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    container.Resources.Add(resource);
                    container.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(Resource resource)
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    if (container.Users.Any())
                    {
                        container.Entry(resource).State = EntityState.Modified;
                        container.SaveChanges();
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(Guid id)
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    var currentResource = this.GetCurrentResource(id);
                    if (currentResource == default(Resource))
                        return false;
                    container.Resources.Remove(currentResource);
                    container.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Resource GetCurrentResource(Guid id)
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    var currentResource = container.Resources
                                                   .FirstOrDefault(resource =>
                                                                   resource.ResourceId == id);
                    if (currentResource == default(Resource))
                        return null;
                    return currentResource;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool SetStatus(Guid id, bool _lock)
        {
            Resource resource = this.GetCurrentResource(id);
            if (resource == null)
                return false;
            resource.Locked = _lock;
            return this.Update(resource);
        }

        public IEnumerable<Resource> GetAll(bool activeOnly = false)
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    if (container.Resources == null || !container.Resources.Any())
                        return null;
                    if (activeOnly)
                        return container.Resources
                                        .Where(resource => resource.Locked)
                                        .ToList();
                    return container.Resources.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string GetResourceName(Guid id)
        {
            var currentResource = GetCurrentResource(id);
            if (currentResource == null)
                return string.Empty;
            return currentResource.Name;
        }

        public Guid GetResourceId(string name)
        {
            Resource res = GetAll().FirstOrDefault(resource => resource.Name == name);
            if (res == default(Resource))
                return Guid.Empty;
            return res.ResourceId;
        }
    }
}